package Dao;

import DataClass.Achievement;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class AchievementDao {

    private static PreparedStatement getAchievementStatement = null;

    public static List<Achievement> getAchievement() throws SQLException {
        List<Achievement> list = new ArrayList<Achievement>();
        ResultSet data;
        try{
            data = AchievementDao.getAchievementData();
        }catch (SQLException e){
            System.out.println("专利查询失败 from Dao");
            throw new SQLException();
        }

        while(data.next()){
            Integer aNo = data.getInt("Ano");

            String aTime = data.getString("ATime");
            String aId = data.getString("Aid");
            String aName = data.getString("Aname");
            String aProposer = data.getString("Apower");
            String aInventor = data.getString("Apersons");
            String aState = data.getString("Astate");
            list.add(new Achievement(aNo, aTime, aId, aName, aProposer, aInventor, aState));
        }

        // 释放资源
        JBDCUtil.closeResultSet(data);

        return list;
    }

    /**
     * 获取专利信息
     */
    public static ResultSet getAchievementData() throws SQLException {

        if(getAchievementStatement == null)
        {
            String sql = "select * from achievement";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            getAchievementStatement = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet data = getAchievementStatement.executeQuery();
        JBDCUtil.commit();
        return data;
    }
}
